package com.farm.wcp.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.farm.core.page.ViewMode;
import com.farm.core.time.TimeTool;
import com.farm.learn.domain.ex.PlayInfo;
import com.farm.learn.service.PlaytimeServiceInter;
import com.farm.parameter.FarmParameterService;
import com.farm.web.WebUtils;

/**
 * 播放状态
 * 
 * @author autoCode
 * 
 */
@RequestMapping("/playing")
@Controller
public class PlayWebController extends WebUtils {
	@Resource
	private PlaytimeServiceInter playTimeServiceImpl;

	/**
	 * 播放中事件记录
	 * 
	 * @param houid
	 * @param session
	 * @return
	 */
	@RequestMapping("/live")
	@ResponseBody
	public Map<String, Object> live(String hourid, int process, HttpSession session, HttpServletRequest request) {
		ViewMode view = ViewMode.getInstance();
		try {
			PlayInfo info = null;
			if (getCurrentUser(session) == null) {
				if (FarmParameterService.getInstance().getParameterBoolean("config.playing.anonymous.able")) {
					info = PlaytimeServiceInter.playingCache
							.get(PlayInfo.getKey(getCurrentIp(request), hourid, TimeTool.getTimeDate8()));
					if (info == null) {
						info = new PlayInfo(getCurrentIp(request), hourid, TimeTool.getTimeDate8());
					}
				} else {
					return ViewMode.getInstance().returnObjMode();
				}
			} else {
				info = PlaytimeServiceInter.playingCache
						.get(PlayInfo.getKey(getCurrentUser(session).getId(), hourid, TimeTool.getTimeDate8()));
				if (info == null) {
					info = new PlayInfo(getCurrentUser(session).getId(), hourid, TimeTool.getTimeDate8());
				}
			}
			//增加播放时长
			info.playing(1);
			//播放到的百分比
			info.setCurentProcess(process);
			PlaytimeServiceInter.playingCache.put(info.getKey(), info);
			view.putAttr("info", info);
			return view.returnObjMode();
		} catch (Exception e) {
			return view.setError(e.getMessage(), e).returnObjMode();
		}
	}

}
